/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.unipar.oficina.controle.cadastros;

import br.com.unipar.oficina.modelo.cadastros.Cargo;
import br.com.unipar.oficina.service.cadastros.CargoService;
import java.io.IOException;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Thiago Marques
 */
@Controller
@Scope("request")
@Named("cargoBean")
public class CargoBean {

    private final CargoService cargoService;
    private Cargo cargo = new Cargo();
    private List<Cargo> cargos;
    private String cargoConsulta;

    @Autowired
    public CargoBean(CargoService cargoService) {
        this.cargoService = cargoService;
    }

    public String novo() {
        cargo = new Cargo();
        return "/gestao/cargoForm.jsf";
    }

    public void salvar(ActionEvent event) {
        try {
            cargoService.salvar(cargo);
            FacesContext.getCurrentInstance().getExternalContext().redirect("cargoLista.jsf");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void consultar(ActionEvent event) {
        cargos = cargoService.consultar(cargoConsulta);
    }

    public void excluir() {
        cargoService.excluir(cargo);
        cargos.remove(cargo);
    }
    
    public String editar() {
        cargo = cargoService.getById(cargo.getIdCargo());
        return "/gestao/cargoForm.jsf";
    }    

    public Cargo getCargo() {
        return cargo;
    }

    public void setCargo(Cargo cargo) {
        this.cargo = cargo;
    }

    public List<Cargo> getCargos() {
        return cargos;
    }

    public void setCargos(List<Cargo> cargos) {
        this.cargos = cargos;
    }

    public String getCargoConsulta() {
        return cargoConsulta;
    }

    public void setCargoConsulta(String cargoConsulta) {
        this.cargoConsulta = cargoConsulta;
    }



    
}
